﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using EMPower.Utility;
using BLL.Common;
using BLL;

namespace App
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            a_logotu.HRef = BLL.Common.Contexts.AppPath + "/Index.aspx";
        }

        protected void Btn_OK_Click(object sender, EventArgs e)
        {
            #region 验证码检验
            string js = "<script type=\"text/javascript\">checkvalidateCode('{0}');</script>";
            if (string.IsNullOrEmpty(this.txt_validateCode.Text))
            {
                Page.RegisterStartupScript("", string.Format(js, "1"));
                return;
            }
            else
            {
                if (Session["ValidateCode"] == null)
                {
                    Page.RegisterStartupScript("", string.Format(js, "2"));
                    return;
                }

                if (Session["ValidateCode"].ToString().ToLower() != this.txt_validateCode.Text.Trim().ToLower())
                {
                    Page.RegisterStartupScript("", string.Format(js, "3"));
                    return;
                }
            }
            UserPassportInfo userInfo = getUserProfile();

            UserPassportInfo user = BUserPassport.getInstance().GetUserPassportInfo(userInfo.UserName);
            if (user != null && user.UserPassportId > 0)
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "用户名已经注册");
                return;
            }

            if (userInfo == null || userInfo.UserPassportId == 0)
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "注册失败，请重试");
                return;
            }
            try
            {
                BLL.BUserPassport.getInstance().Insert(userInfo);
                BLL.BUserProfile.getInstance().Insert(userInfo.ProfileInfo);
            }
            catch (Exception ex)
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, ex.Message);
            }
            Session["LoginUserInfo"] = userInfo;
            //Community.Utility.JavaScriptHelper.MessageBox(this, "注册成功！即将进入首页");
            Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "注册成功！即将进入首页", Contexts.AppPath + "/Index.aspx");
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.location='"+Contexts.AppPath+"/Index.aspx'</script>");
            #endregion
        }

        private UserPassportInfo getUserProfile()
        {
            UserProfileInfo current = new UserProfileInfo();
            UserPassportInfo loginInfo = new UserPassportInfo();
            loginInfo.UserPassportId = IdHelper.GetLongId();
            loginInfo.UserName = this.txt_userName.Text;
            loginInfo.Status = 1;
            loginInfo.Password = this.txt_password.Text;
            loginInfo.LoginCount = 0;
            loginInfo.LastLogin = DateTime.Now;
            

            loginInfo.DateCreated = DateTime.Now;

            current.UserPassportId = loginInfo.UserPassportId;
            current.DateCreated = DateTime.Now;
            current.Email = this.txt_email.Text;
            current.Mobile = txt_PhoneNumber.Text;
            current.UserType = 1;
            current.LastUpdate = DateTime.Now;
            current.IDCardNum = this.txt_IdNum.Text;
            current.LastUpdate = DateTime.Now;
            current.Birthday = new DateTime(1980, 1, 1);

            if (file != null && file.PostedFile != null && !string.IsNullOrEmpty(file.PostedFile.FileName))
            {
                //上传图片

                int filelenth = file.PostedFile.ContentLength;
                int fileSize = Convert.ToInt32((filelenth) / 1024);
                if (fileSize > 5000)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('图片大小不超过5M');</script>");
                    return null;
                }
                string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
                string strFileName = IdHelper.GetLongId().ToString() + "_" + DateTime.Now.Millisecond.ToString() + fileType;
                try
                {
                    string strNewFileName = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Upload) + strFileName;
                    file.SaveAs(strNewFileName);
                }
                catch (Exception ex)
                {
                    Community.Utility.JavaScriptHelper.MessageBox(this, ex.Message);
                }
                current.IDCardImg = strFileName;
            }
           
            loginInfo.ProfileInfo = current;


            return loginInfo;
        }
    }
}